﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;
using System.Linq;

namespace LCA.Community.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for QuestionsRepositoryTest and is intended
    ///to contain all QuestionsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class QuestionsRepositoryTest : BaseTestClass
    {
        private UsersFactory _usersFactory = new UsersFactory();
        private QuestionsFactory _questionsFactory = new QuestionsFactory();
        private AnswersFactory _answersFactory = new AnswersFactory();
        private List<User> _users;
        private List<Question> _questions;
        private List<Answer> _answers;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            _users = _usersFactory.CreateList(5);
            _questions = _questionsFactory.CreateList(10, _users[0].RowKey);
            _answers = _answersFactory.CreateList(10, _questions[0].RowKey, _users[0].RowKey);

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users.ForEach(user => usersRepository.Add(user));

            var questionsRepository = _unityContainer.Resolve<IQuestionsRepository>();
            _questions.ForEach(question => questionsRepository.Add(question));

            var answersRepository = _unityContainer.Resolve<IAnswersRepository>();
            _answers.ForEach(answer => answersRepository.Add(answer));
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var answersRepository = _unityContainer.Resolve<IAnswersRepository>();
            var questionsRepository = _unityContainer.Resolve<IQuestionsRepository>();
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();

            answersRepository.DeleteTestEntities();
            questionsRepository.DeleteTestEntities();
            usersRepository.DeleteTestEntities();
        }  

        /// <summary>
        ///A test for GetQuestionsPage
        ///</summary>
        [TestMethod()]
        public void GetQuestionsPageTest()
        {
            var target = _unityContainer.Resolve<IQuestionsRepository>();

            var page = 0;
            var pageSize = 3;

            var expected = _questions.OrderByDescending(q => q.Timestamp).SkipWhile((q,i) => i < page*pageSize).Take(pageSize).ToList();
            var actual = target.GetQuestionsPage(page, pageSize);

            CollectionAssert.AreEquivalent(expected, actual);

            page = 1;

            expected = _questions.OrderByDescending(q => q.Timestamp).SkipWhile((q, i) => i < page * pageSize).Take(pageSize).ToList();
            actual = target.GetQuestionsPage(page, pageSize);

            CollectionAssert.AreEquivalent(expected, actual);
        }
    }
}
